package com.runchinaup.textapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.runchinaup.textapp.R;
import com.runchinaup.textapp.audioRecord.AudioRecordDemo;
import com.runchinaup.textapp.dialog.BeforeMeasureDialog;
import com.runchinaup.textapp.domain.TextData;

public class MainActivity extends Activity {

    private BeforeMeasureDialog beforeMeasureDialog = null;

    private TextView centerText;
    private ImageView left;
    //是否监听现在音量
    private boolean click = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        //初始化
        initView();

        //一直循环的动画
        //makeAnimation();

        //字节数据测试
        //makeByte();

        //自定义NotificationManager        注意手机开启通知的权限
        //makeNotificationManager();

        //testAb();

        //一个在旋转的星光
//        makeUniverse();

        //获取音量
        getMyVolume();

        new TextData().text();

    }


    private void initView() {


        centerText = findViewById(R.id.centerText);
        centerText.setText("这是标题");

        left = findViewById(R.id.leftIcon);
        left.setImageResource(R.mipmap.menu);

        byte[] bytes = new byte[]{1, 0, 0, 1, 1, 1, 1};
//        makeWeekData(bytes);

    }

//    private byte week = 0;

//    private void makeWeekData(byte[] bytes) {
//        for (int i = 0; i < 7; i++) {
//            if (bytes[i] == 1) {
//                week += Math.pow(2, i);
//            }
//        }
//        Log.e("makeWeekData: ", week + "");
//        everyDay(week);
//    }
//
//    private byte[] everyDay(byte weekData) {
//        byte[] every = new byte[7];
//        for (int i = 0; i < 7; i++) {
//            every[i] = (byte) (weekData % 2);
//            weekData = (byte) (weekData / 2);
//            Log.e("everyDay: i", every[i] + "");
//        }
////        StringBuilder sb = new StringBuilder();
////        for (byte b : every) {
////            sb.append(String.format("%01d", (b & 0xff)));
////        }
////        Log.e("everyDay: ", sb.toString());
//        return every;
//    }

    private void getMyVolume() {

        Button button = findViewById(R.id.volume);
        final TextView showVolunme = findViewById(R.id.show_voilume);

        final AudioRecordDemo audioRecordDemo = new AudioRecordDemo(new AudioRecordDemo.AudioCallback() {
            @Override
            public void volumeData(double data) {
                final int number = (int) data;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showVolunme.setText(number + "");
                    }
                });
            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!click) {
                    audioRecordDemo.getNoiseLevel();
                } else {
                    audioRecordDemo.closeAudio();
                    showVolunme.setText("--");
                }
                click = !click;
            }
        });

    }

    private void makeUniverse() {
//        ImageView universe = findViewById(R.id.universe);
//
//        Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
//        LinearInterpolator lin = new LinearInterpolator();
//        animation.setInterpolator(lin);
//        universe.startAnimation(animation);

    }

    private void makeNotificationManager() {
//        // 创建一个NotificationManager的引用
//        final NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
//// 定义Notification的各种属性
//        final Notification.Builder mBuilder = new Notification.Builder(this.getApplicationContext())
//                .setSound(android.provider.Settings.System.DEFAULT_NOTIFICATION_URI)        //
//                .setSmallIcon(R.mipmap.ic_launcher)                                         //设置通知的图标
//                .setTicker("有新消息呢")                                                     //设置状态栏的标题
//                .setContentTitle("这个是标题")                                               //设置标题
//                .setContentText("这个是内容")                                                //消息内容
//                .setDefaults(Notification.DEFAULT_ALL)                                      //设置默认的提示音
//                .setPriority(Notification.PRIORITY_DEFAULT)                                 //设置该通知的优先级
//                .setOngoing(false)                                                          //让通知左右滑的时候不能取消通知
//                .setPriority(Notification.PRIORITY_DEFAULT)                                 //设置该通知的优先级
//                .setWhen(System.currentTimeMillis())                                        //设置通知时间，默认为系统发出通知的时间，通常不用设置
//                .setAutoCancel(true);                                                       //打开程序后图标消失
////处理点击Notification的逻辑
//        Intent resultIntent = new Intent(this, TestActivity.class);
//        resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);           //添加为栈顶Activity
//        resultIntent.putExtra("what", 5);
//        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 5, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//        mBuilder.setContentIntent(resultPendingIntent);
////发送
//
//        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                Toast.makeText(MainActivity.this, "显示通知吧，求求你", Toast.LENGTH_SHORT).show();
//                mNotificationManager.notify(1, mBuilder.build());
//            }
//        });
    }

    private void makeByte() {
//        byte[] data = new byte[]{0x55, 0x00, 0x34, 0x04, 0x01, 0x02, 0x00, 0x4b, 0x00};
//        byte[] length = new byte[]{0x03, 0x00};
//        byte[] value = new byte[data.length + length.length];
//        System.arraycopy(length, 0, value, 0, length.length);
//        System.arraycopy(data, 0, value, length.length, data.length);
//
//        StringBuilder builder = new StringBuilder();
//        for (byte b : value) {
////            builder.append(String.format("%02d", (b & 0xff)));
//            builder.append(String.format("%02x", (b & 0xff)));
//        }
//        Log.e("debug_read", builder.toString());
    }

    //一直循环的动画
    private void makeAnimation() {
//        beforeMeasureDialog = new BeforeMeasureDialog(this);
//        beforeMeasureDialog.show();
    }

    //测试回调
    private void testAb() {
//        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                startActivityForResult(new Intent(MainActivity.this, TestActivity.class), 000);
//            }
//        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if (requestCode == 000) {
//            Helper helper = new Helper() {
//                @Override
//                public void show() {
//                    Toast.makeText(MainActivity.this, "MainActivity", Toast.LENGTH_SHORT).show();
//                    Log.e("debug_main", "MainActivity");
//                }
//            };
//            helper.interest();
//        }
    }

    public void onLeftClick(View view) {

        Toast.makeText(this, "点击左上角", Toast.LENGTH_SHORT).show();

    }

    public void onRightClick(View view) {

        Toast.makeText(this, "点击右上角", Toast.LENGTH_SHORT).show();

    }
}
